<template>
	<view class="content page">
		<view class="duild_con">
			<!-- <u-form :model="form" ref="uForm"> -->
			<view class="duild_list" @click="goDuildInfo()">
				<view class="duild_intro">
					<view class="intro">
						<text>绿化区域</text>
						<text></text>
					</view>
				</view>

				<!-- <u-form-item prop="areaName"> -->
				<view class="gt">
					<text>{{form.areaName}}</text>
					<text>></text>
				</view>
				<!-- </u-form-item> -->
			</view>
			<view class="record">
				<view class="record_con">
					<text>天气</text>
				</view>
				<view class="record_pass">
					<view :class="responsibilityAc==1?'pass_select':''" @click="changeResponsibility(1)">
						阴
					</view>
					<view :class="responsibilityAc==0?'pass_select':''" @click="changeResponsibility(0)">
						晴
					</view>
					<view :class="responsibilityAc==2?'pass_select':''" @click="changeResponsibility(2)">
						小雨
					</view>
					<view :class="responsibilityAc==3?'pass_select':''" @click="changeResponsibility(3)">
						雷雨
					</view>
				</view>
			</view>
			<view class="temperature">
				<view class="">
					温度
				</view>
				<u-input v-model="form.temperature" height="40rpx" input-align="right" placeholder-style="fontSize:28rpx;color: #999999;"
				 placeholder="请输入今日温度" />
			</view>

			<view class="record viresc">
				<view class="record_con">
					<text>绿化内容（多选）</text>
				</view>
				<view class="record_pass viresc_pass">
					<view v-for="(item,index) in virescInfo" :key="index" :class="item.value===true?'pass_select':''" @click="changeViresc(index,item.value,item.name)">
						{{item.name}}
					</view>
				</view>
			</view>

			<view class="lhwh_con">
				<view class="">
					备注
				</view>
				<textarea v-model="form.remark" auto-height maxlength="200" placeholder="请输入最多200个字符…"></textarea>
			</view>
			<!-- </u-form> -->
			<view class="lhwh_sub" @click="submit()">
				提交
			</view>
		</view>
		<u-select value-name="id" cancel-color="#999999" confirm-color="#44D94B" label-name="areaName" v-model="show" :list="list" @confirm="confirm"></u-select>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					areaName: '',
					areaId: '',
					weather: '',
					temperature: '',
					remark: '',
					content: []
				},
				rules: {
					areaName: [{
						required: true,
						message: '请选择绿化区域',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}]
				},
				list: [],
				show: false,
				areaName: '',
				areaId: '',
				responsibilityAc: 0,
				temperature: '',
				virescInfo: [{
						name: '浇水',
						value: false
					},
					{
						name: '修剪',
						value: false
					},
					{
						name: '除草',
						value: false
					},
					{
						name: '施肥',
						value: false
					},
					{
						name: '喷药',
						value: false
					},
					{
						name: '切边',
						value: false
					},
					{
						name: '清除残枝',
						value: false
					},
					{
						name: '清除垃圾',
						value: false
					},
				]

			};
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onShow() {
			this.reset()
		},
		onReady() {
			// this.$refs.uForm.setRules(this.rules);
		},
		onLoad(option) {
			this.buiGreenAreaList()
		},
		methods: {
			reset() {

				this.form = {}
				this.virescInfo = [{
						name: '浇水',
						value: false
					},
					{
						name: '修剪',
						value: false
					},
					{
						name: '除草',
						value: false
					},
					{
						name: '施肥',
						value: false
					},
					{
						name: '喷药',
						value: false
					},
					{
						name: '切边',
						value: false
					},
					{
						name: '清除残枝',
						value: false
					},
					{
						name: '清除垃圾',
						value: false
					},
				]
			},
			getTitle(title) {
				uni.setNavigationBarTitle({
					title: title
				});
			},
			buiGreenAreaList() {
				//联网加载数据
				
				this.apis.greencleanApi.buiGreenAreaList().then(res => {
					this.list = res.rows
				}).catch(() => {
					//联网失败的回调,隐藏下拉刷新的状态
				})
			},
			goDuildInfo(i) {
				this.show = true
			},
			confirm(e) {
				this.form.areaName = e[0].label
				this.form.areaId = e[0].value
			},
			changeResponsibility(value) {
				this.responsibilityAc = value
				switch (value) {
					case 1:
						this.form.weather = '阴'
						break
					case 2:
						this.form.weather = '小雨'
						break
					case 3:
						this.form.weather = '雷雨'
						break
					case 0:
						this.form.weather = '晴'
						break
				}
			},
			changeViresc(index, val, name) {
				const content = []
				if (val) {
					this.virescInfo[index].value = false
				} else {
					this.virescInfo[index].value = true
				}
				for (let i = 0; i < this.virescInfo.length; i++) {
					const element = this.virescInfo[i];
					if (element.value === true) {
						content.push(element.name)
					} else {
						content.splice(i, 1)
					}
				}
				console.log(this.form.content)
				this.form.content = this.fun(content)
			},
			fun(arr) {
				for (let i = 0; i < arr.length; i++) {
					for (let j = i + 1; j < arr.length; j++) {
						if (arr[i] == arr[j]) {
							arr.splice(j, 1);
						}
					}
				}
				return arr;
			},
			submit() {
				console.log(this.form.temperature)
				if(this.form.areaId===undefined || this.form.temperature===undefined){
					this.showToast("填写完内容！")
					return
				}else {
					const content = JSON.parse(JSON.stringify(this.form.content))
					this.form.content = content.join(',')
					//联网加载数据
					this.apis.greencleanApi.addBuiGreenRecord(this.form).then(res => {
						uni.navigateTo({
							url: './successfully?id='+res.msg
						})
						this.form = {}
					}).catch(() => {
						//联网失败的回调,隐藏下拉刷新的状态
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		padding: 0;
	}

	.content {
		.duild_con {
			overflow-y: auto;
			height: 1160rpx;
		}

		.duild_list {
			padding: 30rpx;
			box-sizing: border-box;
			background: #FFFFFF;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;

			border-radius: 12rpx;
			/deep/ .u-form-item {
				line-height: 20rpx;
			}

			.duild_intro {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				color: #333333;
				font-size: 26rpx;

				.intro {
					display: flex;
					font-size: 30rpx;
					justify-content: space-between;
					font-weight: 500;
				}
			}

			.gt {
				color: #999999;
				font-size: 26rpx;

				text:nth-child(1) {
					font-size: 28rpx;
					color: #333333;
					margin-right: 10rpx;
				}
			}
		}

		.record {
			display: flex;
			flex-direction: column;
			background: #FFFFFF;
			padding: 30rpx;
			margin-bottom: 20rpx;

			border-radius: 12rpx;
			.record_con {
				font-size: 30rpx;
				font-weight: 500;

				text:nth-child(1) {
					color: #333333;
				}

				text:nth-child(2) {
					color: #999999;
				}
			}

			.record_pass {
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;

				view {
					padding: 0;
					font-size: 28rpx;
					color: #666666;
					width: 146rpx;
					height: 70rpx;
					border-radius: 4rpx;
					line-height: 70rpx;
					text-align: center;
					background: rgba($color: #DDDDDD, $alpha: 0.35);
				}

				view:nth-child(1) {
					// margin-right: 20rpx;
				}

				.pass_select {
					box-sizing: border-box;
					color: #44D94B;
					background: rgba(68, 217, 75, 0.1);
					border: 1rpx solid #44D94B !important;
				}
			}
		}

		.viresc {
			padding-bottom: 10rpx;

			.viresc_pass {
				display: flex;
				flex-wrap: wrap;

				view {

					margin-bottom: 20rpx;
				}
			}
		}

		.temperature {
			padding: 30rpx;
			background: #FFFFFF;
			display: flex;
			justify-content: space-between;
			align-items: center;

			view:nth-child(1) {
				font-size: 30rpx;
				font-weight: 500;
				color: #333333;
			}
		}


		.lhwh_con {
			display: flex;
			flex-direction: column;
			background: #FFFFFF;
			padding: 40rpx 30rpx;
			margin-bottom: 20rpx;
			align-items: flex-start;
			border-radius: 12rpx;

			view {
				font-size: 30rpx;
				font-weight: 500;
				color: #333333;
			}

			/deep/ .uni-textarea-wrapper {
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				line-height: 40rpx;
			}

			/deep/ .uni-textarea-placeholder {
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 40rpx;
			}

			textarea {
				margin-top: 20rpx;
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				min-height: 200rpx;
			}
		}

		.lhwh_sub {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			padding: 28rpx 0;
			background: #44D94B;
			font-size: 34rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			@include flexCenter
		}
	}
</style>
